package com.sad.we11cis;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignUpActivity extends Activity implements OnClickListener{
	
	Button confirmButton;
	EditText signIDText, signPassText, signRePassText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sign_up);
		
		confirmButton = (Button)findViewById(R.id.btnSUConfirm);
		signIDText = (EditText)findViewById(R.id.signupID);
		signPassText = (EditText)findViewById(R.id.signupPass);
		signRePassText = (EditText)findViewById(R.id.signupRePass);
		
		confirmButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnSUConfirm:
			if(signPassText.getText().toString().equals(signRePassText.getText().toString())){
				SignUpTask signUpTask = new SignUpTask();
				signUpTask.execute();
			}
			else{
				Toast wrongSignUpMessage = Toast.makeText(getApplicationContext(), "Check the password again", Toast.LENGTH_SHORT);
				wrongSignUpMessage.show();
			}
			break;
		}
	}
	
	private class SignUpTask extends AsyncTask<Void, String, Void>{
		
		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				URL url = new URL("http://192.168.101.10:8080/Weening11evenServer/CommunicateServlet");
				URLConnection connection = url.openConnection();
				
				//send ID and password to server
				String idString = signIDText.getText().toString();
				String passwordString = signPassText.getText().toString();
				connection.setDoOutput(true);
				OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
				out.write("For SignUp/" + idString + "/" + passwordString);
				out.close();
				
				//receive result of sign up from server
				String registerResult = "";
				BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				registerResult = in.readLine();
				in.close();
				
				if(registerResult.equals("Success Register")){
					publishProgress("Success");
					
					Intent gotoMain = new Intent(SignUpActivity.this, MainActivity.class);
					startActivity(gotoMain);
				}
				else if(registerResult.equals("Fail Register")){
					publishProgress("Fail");
				}
				
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onProgressUpdate(String... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);

			if(values[0].equals("Success")){
				Toast successRegisterMessage = Toast.makeText(getApplicationContext(), "Register success", Toast.LENGTH_SHORT);
				successRegisterMessage.show();
			}
			else if(values[0].equals("Fail")){
				Toast failRegisterMessage = Toast.makeText(getApplicationContext(), "Register failed", Toast.LENGTH_SHORT);
				failRegisterMessage.show();
			}
		}
		
	}
}
